package cn.lystudio.rohmtimeopwide_background_v11.controller.appApi;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.RecordPO;
import cn.lystudio.rohmtimeopwide_background_v11.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app-api/record")
@CrossOrigin
@SaIgnore
@Slf4j
public class AppRecordController {
    private RecordService recordService;
    @Autowired
    @Lazy
    public void setRecordService(RecordService recordService) {
        this.recordService = recordService;
    }
    @PostMapping("/insertRecord")
    public SaResult appInsertRecord(@RequestBody RecordPO recordPO){
        int res = recordService.AppInsertRecord(recordPO);
        if(res<1){
            return SaResult.error("数据写入失败");
        }
        return SaResult.ok();
    }

    @GetMapping("/getUnfinishedRecord")
    public SaResult getUnfinishedRecord(String machineNo){
        List<RecordPO> unfinishedRecordList =  recordService.getUnfinishedRecord(machineNo);
        if(CollUtil.isEmpty(unfinishedRecordList)) return SaResult.ok();
        return SaResult.data(unfinishedRecordList);
    }

    @PostMapping("/uptUnfinishedRecord")
    public SaResult uptUnfinishedRecord(@RequestBody RecordPO recordPO){
        if(StrUtil.isEmpty(recordPO.getUserCode())) recordPO.setUserCode(null);

        int res = recordService.uptUnfinishedRecord(recordPO);
        if( res>0 ){
            return SaResult.ok();
        }
        return SaResult.error("未找到对应开始作业");
    }

    @GetMapping("/getRecordByRecordID")
    public SaResult getRecordByRecordID(int recordID){
        RecordPO recordPO = recordService.getRecordByRecordID(recordID);
        if(ObjectUtil.isEmpty(recordPO)){
            return SaResult.error("获取数据ID异常");
        }
        return SaResult.data(recordPO);
    }
    @PostMapping("/uptRecordByRecordID")
    public SaResult uptRecordByRecordID(@RequestBody RecordPO recordPO){
        if(StrUtil.isEmpty(recordPO.getUserCode())) recordPO.setUserCode(null);
        int res = recordService.uptRecordByRecordID(recordPO);
        if( res<1  ) return SaResult.error("更新失败");
        return SaResult.ok();
    }


        @PostMapping("/delRecordByRecordID")
    public SaResult delRecordByRecordID(@RequestBody RecordPO record){
        int res = recordService.delRecord(record.getRecordID());
        if(res<1) return SaResult.error("删除失败");
        return SaResult.ok();
    }

}
